package main

import (
	"context"
	"errors"
	"hxz_rpc/hasic/globar"
	user "hxz_rpc/kitex_gen/hxz_rpc/user"
	"hxz_rpc/model"
	"hxz_rpc/until"
)

// UserServiceImpl implements the last service interface defined in the IDL.
type UserServiceImpl struct{}

// GetUserRegister implements the UserServiceImpl interface.
func (s *UserServiceImpl) GetUserRegister(ctx context.Context, req *user.GetUserRegisterReq) (resp *user.GetUserRegisterResp, err error) {
	// TODO: Your code here...
	var u model.User
	u.Mobile = req.Mobile
	if err := u.GetOnfile(globar.DB); err != nil {
		return nil, errors.New("查询失败")
	}
	u.Password = until.Md5(req.Password)
	if err := u.Create(globar.DB); err != nil {
		return nil, errors.New("添加失败")
	}
	resp = &user.GetUserRegisterResp{
		Id: int64(u.ID),
	}
	return
}

// GetUserList implements the UserServiceImpl interface.
func (s *UserServiceImpl) GetUserList(ctx context.Context, req *user.GetUserListReq) (resp *user.GetUserListResp, err error) {
	// TODO: Your code here...
	var u model.User
	globar.DB.Find(&u)
	resp = &user.GetUserListResp{
		Mobile:   u.Mobile,
		Password: u.Password,
		Status:   u.Status,
		Id:       int64(u.ID),
	}
	return
}

// GetUserLogin implements the UserServiceImpl interface.
func (s *UserServiceImpl) GetUserLogin(ctx context.Context, req *user.GetUserLoginReq) (resp *user.GetUserLoginResp, err error) {
	// TODO: Your code here...
	var u model.User
	u.Mobile = req.Mobile
	if err := u.GetOnfile(globar.DB); err != nil {
		return nil, errors.New("查询失败")
	}
	if u.Password != until.Md5(req.Password) {
		return nil, errors.New("两次密码不一致")
	}

	resp = &user.GetUserLoginResp{
		Id: int64(u.ID),
	}
	return

}
